package com.strawcom.charmring.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.TextView;

import com.strawcom.charmring.BaseFragment;
import com.strawcom.charmring.R;
import com.strawcom.charmring.ui.ExerciseRankActivity;
import com.strawcom.charmring.ui.ExerciseSettingsActivity;
import com.strawcom.charmring.ui.MainActivity;
import com.strawcom.charmring.util.AnimUtil;

/**
 * @author WangPeng
 *
 */
public class ExerciseFragment extends BaseFragment {

	private final int WHICH_DAILY = 1;
	private final int WHICH_WEEK = 2;
	private MainActivity holderActivity;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			holderActivity = (MainActivity) activity;
		}
	}

	private ExerciseDailySummaryFragment dailySummaryFragment;
	private ExerciseWeekSummaryFragment weekSummaryFragment;
	private FragmentManager fragmentManager;

	private View layTopCenterMenu, layTopRank, layTopInfo, layTopSettings;
	private View layTopRightMenu;
	private TextView txRecordType;
	
	private boolean isDailySummary = true;
	private String[] times;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		showFragment(WHICH_DAILY);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_exercise, container,
				false);

		fragmentManager = getChildFragmentManager();

		layTopCenterMenu = inflater.inflate(
				R.layout.view_exercise_top_menu_center, null);
		layTopRightMenu = inflater.inflate(
				R.layout.view_exercise_top_menu_right, null);
		layTopRightMenu.setOnClickListener(onSummarySwitchClickListener);
		txRecordType = (TextView) layTopRightMenu
				.findViewById(R.id.tx_record_type);
		

		times = getResources().getStringArray(R.array.exercise_summary_time);
		txRecordType.setText(times[0]);
		layTopRank = layTopCenterMenu.findViewById(R.id.lay_movement_rank);
		layTopRank.setOnClickListener(onTopMenuClickListener);
		layTopInfo = layTopCenterMenu.findViewById(R.id.lay_movement_info);
		layTopInfo.setOnClickListener(onTopMenuClickListener);
		layTopSettings = layTopCenterMenu
				.findViewById(R.id.lay_movement_settings);
		layTopSettings.setOnClickListener(onTopMenuClickListener);

		resetTitle();

		return view;
	}

	private OnClickListener onSummarySwitchClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			isDailySummary = !isDailySummary;
			txRecordType.clearAnimation();
			txRecordType.startAnimation(AnimUtil.init()
					.addAlpha(1f, 0.1f, 0, 100, null, 0).create());
			txRecordType.setText(isDailySummary ? times[0] : times[1]);
			txRecordType.startAnimation(AnimUtil.init()
					.addAlpha(0.1f, 1f, 100, 100, null, 0).create());

			showFragment(isDailySummary ? WHICH_DAILY : WHICH_WEEK);
		}
	};

	private OnClickListener onTopMenuClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.lay_movement_rank:
				layTopRank.startAnimation(AnimUtil.init()
						.addAlpha(1f, 0.1f, 0, 200, null, Animation.REVERSE)
						.create());
				actionStart(ExerciseRankActivity.class);
				break;
			case R.id.lay_movement_info:
				break;
			case R.id.lay_movement_settings:
				layTopSettings.startAnimation(AnimUtil.init()
						.addAlpha(1f, 0.1f, 0, 200, null, Animation.REVERSE)
						.create());
				actionStart(ExerciseSettingsActivity.class);
				break;
			}
		}
	};

	public void resetTitle() {
		if (holderActivity != null) {
			holderActivity.titleView.setCenterView(layTopCenterMenu);
			holderActivity.titleView.setRightView(layTopRightMenu);
		}
	}

	private void hideFragments(FragmentTransaction transaction) {
		if (dailySummaryFragment != null) {
			transaction.hide(dailySummaryFragment);
		}
		if (weekSummaryFragment != null) {
			transaction.hide(weekSummaryFragment);
		}
	}

	private void showFragment(int which) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		hideFragments(transaction);
		switch (which) {
		case WHICH_DAILY:
		default:
			if (dailySummaryFragment == null) {
				dailySummaryFragment = new ExerciseDailySummaryFragment();
				transaction.add(R.id.lay_content, dailySummaryFragment);
			} else {
				transaction.show(dailySummaryFragment);
			}
			break;
		case WHICH_WEEK:
			if (weekSummaryFragment == null) {
				weekSummaryFragment = new ExerciseWeekSummaryFragment();
				transaction.add(R.id.lay_content, weekSummaryFragment);
			} else {
				transaction.show(weekSummaryFragment);
			}
			break;
		}
		transaction.commit();
	}

	
}
